//////////////////////////////////////////
// EMITTER CLASS						//
//////////////////////////////////////////
/**
 * Emitters emit a radial force which can either attract or repel
 * biods. Emitters have various function typs (linear, log, exp)
 * and can stack with each other.
 */
class Emitter {
    constructor(position, strength, radius, functionType, repel) {
        this.position = position;
        this.strength = strength;
        this.radius = radius;
        this.functionType = functionType;
        this.repel = repel;
    }

    update(flock) {
        //Affect any biods within range.
        for (var i = 0; i < flock.length; i++) {
            var distance = flock[i].position.getDistance(this.position);

            if (distance <= this.radius) {
                var subAmount = 0;

                switch (this.functionType) {
                    case "linear":
                        subAmount = Math.min(this.strength, distance);
                        break;

                    case "log":
                        subAmount = Math.max(0, Math.log(distance));
                        break;

                    case "exp":
                        subAmount = Math.max(0, distance * distance);
                        break;
                }

                var influenceStrength = this.strength - subAmount;

                if (influenceStrength < 0) influenceStrength = 0;

                //Add to the biod's current tick.
                if (this.repel)
                    flock[i].emitterInfluences.push(
                        this.position
                            .getDirectionTo(flock[i].position)
                            .scale(influenceStrength)
                    );
                else
                    flock[i].emitterInfluences.push(
                        flock[i].position
                            .getDirectionTo(this.position)
                            .scale(influenceStrength)
                    );
            }
        }
    }
}

export { Emitter };
